package hpu.e4_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hpu.e4_demo.common.Result;
import hpu.e4_demo.entity.Yuyue;
import hpu.e4_demo.mapper.YuyueMapper;
import hpu.e4_demo.service.YuyueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class YuyueServiceImpl extends ServiceImpl<YuyueMapper, Yuyue> implements YuyueService {

    @Autowired
    private YuyueMapper yuyueMapper;

    @Override
    public Result<Yuyue> addYuyue(Yuyue yuyue) {
        try {
            // 检查时间冲突
            QueryWrapper<Yuyue> wrapper = new QueryWrapper<>();
            wrapper.eq("cheliang_id", yuyue.getCheliangId())
                    .le("start_time", yuyue.getEndTime())
                    .ge("end_time", yuyue.getStartTime())
                    .ne("status", 3); // 排除已取消的预约

            List<Yuyue> conflicts = yuyueMapper.selectList(wrapper);
            if (!conflicts.isEmpty()) {
                return Result.fail(null);
            }

            yuyue.setStatus(0); // 默认待审核状态
            yuyue.setCreateTime(new Date()); // 设置创建时间
            yuyue.setUpdateTime(new Date()); // 设置更新时间
            boolean success = this.save(yuyue);
            if (success) {
                return Result.ok(yuyue);
            } else {
                return Result.fail(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    @Override
    public Result<String> cancelYuyue(Integer id) {
        try {
            Yuyue yuyue = this.getById(id);
            if (yuyue == null) {
                return Result.fail("预约不存在");
            }

            if (yuyue.getStatus() == 3) {
                return Result.fail("预约已取消");
            }

            yuyue.setStatus(3); // 设置为已取消
            boolean success = this.updateById(yuyue);
            if (success) {
                return Result.ok("取消成功");
            } else {
                return Result.fail("取消失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    @Override
    public Result<List<Yuyue>> getYuyueList(Integer status) {
        try {
            IPage<Yuyue> page = new Page<>(1, 1000);
            IPage<Yuyue> result = yuyueMapper.findAll(page, status);
            return Result.ok(result.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    @Override
    public Result<List<Yuyue>> getYuyueByCheliangId(Integer cheliangId) {
        try {
            List<Yuyue> list = yuyueMapper.findByCheliangId(cheliangId);
            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    @Override
    public Result<List<Yuyue>> getYuyueByUserId(Integer userId) {
        try {
            List<Yuyue> list = yuyueMapper.findByUserId(userId);
            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    @Override
    public Result<String> approveYuyue(Integer id) {
        try {
            int count = yuyueMapper.updateStatus(id, 1);
            if (count > 0) {
                return Result.ok("审核通过");
            } else {
                return Result.fail("操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    @Override
    public Result<String> rejectYuyue(Integer id) {
        try {
            int count = yuyueMapper.updateStatus(id, 2);
            if (count > 0) {
                return Result.ok("已拒绝");
            } else {
                return Result.fail("操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }
}